using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using UwinEducation.API.Attributes;
using UwinEducation.Application.Common.Interfaces;
using UwinEducation.Application.Dtos;
using UwinEducation.Shared.Models;

namespace UwinEducation.API.Controllers
{
    /// <summary>
    /// 教师管理控制器
    /// </summary>
    [Authorize]
    //[RequirePermission("teacher.course")] // 指定所需的权限
    [ApiController]
    [Route("api/[controller]/[action]")]
    public class TeacherController : ControllerBase
    {
        private readonly ITeacherService _teacherService;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="teacherService">教师服务接口</param>
        public TeacherController(ITeacherService teacherService)
        {
            _teacherService = teacherService;
        }

        /// <summary>
        /// 获取教师分页列表
        /// </summary>
        /// <param name="pageRequest">分页请求参数</param>
        /// <returns>教师分页列表</returns>
        [HttpPost]
        public async Task<ApiResponse<PagedResult<TeacherDto>>> GetPagedList([FromBody] TeacherPageRequest pageRequest)
        {
            var result = await _teacherService.GetPagedListAsync(pageRequest);
            return ApiResponse<PagedResult<TeacherDto>>.Ok(result);
        }

        /// <summary>
        /// 获取教师分页列表（按是否有排课排序）
        /// </summary>
        /// <param name="pageRequest">分页请求参数</param>
        /// <returns>教师分页列表</returns>
        [HttpPost]
        public async Task<ApiResponse<PagedResult<TeacherDto>>> GetPagedListWithScheduleStatus([FromBody] TeacherAvailabilityPageRequest pageRequest)
        {
            var result = await _teacherService.GetPagedListWithScheduleStatusAsync(pageRequest);
            return ApiResponse<PagedResult<TeacherDto>>.Ok(result);
        }

        /// <summary>
        /// 获取教师详情
        /// </summary>
        /// <param name="request">ID请求参数</param>
        /// <returns>教师详情</returns>
        [HttpPost]
        public async Task<ApiResponse<TeacherDto>> GetById([FromBody] IdRequest request)
        {
            var teacher = await _teacherService.GetByIdAsync(request.Id);
            return ApiResponse<TeacherDto>.Ok(teacher);
        }

        /// <summary>
        /// 创建教师
        /// </summary>
        /// <param name="input">创建教师请求参数</param>
        /// <returns>教师详情</returns>
        [HttpPost]
        [RequirePermission("teacher.course")] // 指定所需的权限
        public async Task<ApiResponse<TeacherDto>> Create([FromBody] CreateTeacherDto input)
        {
            var teacher = await _teacherService.CreateAsync(input);
            return ApiResponse<TeacherDto>.Ok(teacher);
        }

        /// <summary>
        /// 更新教师
        /// </summary>
        /// <param name="input">更新教师请求参数</param>
        /// <returns>教师详情</returns>
        [HttpPost]
        [RequirePermission("teacher.course")] // 指定所需的权限
        public async Task<ApiResponse<TeacherDto>> Update([FromBody] UpdateTeacherDto input)
        {
            var teacher = await _teacherService.UpdateAsync(input);
            return ApiResponse<TeacherDto>.Ok(teacher);
        }

        /// <summary>
        /// 删除教师
        /// </summary>
        /// <param name="request">ID请求参数</param>
        [HttpPost]
        [RequirePermission("teacher.course")] // 指定所需的权限
        public async Task<ApiResponse<object>> Delete([FromBody] IdRequest request)
        {
            await _teacherService.DeleteAsync(request.Id);
            return ApiResponse<object>.Ok(new object());
        }
    }
}